package com.wlcorp.forcazamis.dao.system.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ibatis.dao.client.DaoException;
import com.wlcorp.forcazamis.dao.system.IDepartmentDAO;
import com.wlcorp.forcazamis.domain.system.Department;

public class DepartmentDAOImpl extends HibernateDaoSupport implements
		IDepartmentDAO {

	@SuppressWarnings("unchecked")
	public List getDepartmentList(Department department) throws DaoException{
		return getHibernateTemplate().find("from Department as d where d.deleteFlag=false");
	}

	public Department getDepartmentById(Long departmentId) throws DaoException{
		return (Department)getHibernateTemplate().get(Department.class,departmentId);
	}

	public void deleteDepartment(Department department) throws DaoException{
		department.setDeleteFlag(true);
		getHibernateTemplate().update(department);
	}

	public void saveDepartment(Department department) throws DaoException{
		getHibernateTemplate().saveOrUpdate(department);
	}

	public Department getDepartmentByFlag(Integer flag) throws DaoException {
		try {
			String queryString = "from Department as d where d.deleteFlag=false and d.flag = :flag";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setInteger("flag", flag);
			return (Department)queryObject.uniqueResult();
		} catch (RuntimeException re) {
			throw re;
		}
	}

}
